package com.platform.web.controller.module;

import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.domain.Ztree;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.utils.StringUtils;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.framework.util.ShiroUtils;
import com.platform.module.domain.*;
import com.platform.module.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址资料Controller
 * 
 * @author wangqi
 * @date 2020-06-24
 */
@Controller
@RequestMapping("/module/subscriber")
public class BaseSubscriberController extends BaseController
{
    private String prefix = "module/subscriber";

    @Autowired
    private IBaseSubscriberService baseSubscriberService;

    @Autowired
    private IBaseCalorimeterDataService calorimeterDataService;

    @Autowired
    private IBaseValveDataService valveDataService;

    @Autowired
    private  IBaseRoomTempService roomTempService;
    
    @Autowired
    private  IBaseConcentratorDataService concentratorDataService;

    @Autowired
    private ILinkSubTempService linkSubTempService;

    @Autowired
    private ILinkSubValveService linkSubValveService;

    @Autowired
    private ILinkSubMeterService linkSubMeterService;

    @Autowired
    private ILinkSubConcentratorService linkSubConcentratorService;



    @RequiresPermissions("module:subscriber:view")
    @GetMapping()
    public String subscriber()
    {
        return prefix + "/subscriber";
    }

    /**
     * 查询地址资料树列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/list")
    @ResponseBody
    public List<BaseSubscriber> list(BaseSubscriber baseSubscriber)
    {
        List<BaseSubscriber> list = baseSubscriberService.selectBaseSubscriberList(baseSubscriber);
        return list;
    }

    /**
     * 导出地址资料列表
     */
    @RequiresPermissions("module:subscriber:export")
    @Log(title = "地址资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseSubscriber baseSubscriber)
    {
        List<BaseSubscriber> list = baseSubscriberService.selectBaseSubscriberList(baseSubscriber);
        ExcelUtil<BaseSubscriber> util = new ExcelUtil<BaseSubscriber>(BaseSubscriber.class);
        return util.exportExcel(list, "subscriber");
    }

    /**
     * 新增地址资料
     */
    @GetMapping(value = { "/add/{subscriberId}", "/add/" })
    public String add(@PathVariable(value = "subscriberId", required = false) Long subscriberId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(subscriberId))
        {
            mmap.put("baseSubscriber", baseSubscriberService.getById(subscriberId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存地址资料
     */
    @RequiresPermissions("module:subscriber:add")
    @Log(title = "地址资料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseSubscriber baseSubscriber)
    {
        baseSubscriber.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(baseSubscriberService.insertBaseSubscriber(baseSubscriber));
    }

    /**
     * 修改地址资料
     */
    @GetMapping("/edit/{subscriberId}")
    public String edit(@PathVariable("subscriberId") Long subscriberId, ModelMap mmap)
    {
        BaseSubscriber baseSubscriber = baseSubscriberService.getById(subscriberId);
        mmap.put("baseSubscriber", baseSubscriber);
        return prefix + "/edit";
    }

    /**
     * 修改保存地址资料
     */
    @RequiresPermissions("module:subscriber:edit")
    @Log(title = "地址资料", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseSubscriber baseSubscriber)
    {
        baseSubscriber.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(baseSubscriberService.updateBaseSubscriber(baseSubscriber));
    }

    /**
     * 删除
     */
    @RequiresPermissions("module:subscriber:remove")
    @Log(title = "地址资料", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{subscriberId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("subscriberId") Long subscriberId)
    {
        return toAjax(baseSubscriberService.deleteBaseSubscriberById(subscriberId));
    }

    /**
     * 选择地址资料树
     */
    @GetMapping(value = { "/selectSubscriberTree/{subscriberId}", "/selectSubscriberTree/" })
    public String selectSubscriberTree(@PathVariable(value = "subscriberId", required = false) Long subscriberId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(subscriberId))
        {
            mmap.put("baseSubscriber", baseSubscriberService.getById(subscriberId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载地址资料树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = baseSubscriberService.selectBaseSubscriberTree();
        return ztrees;
    }


    /**
     * 分配热表
     */
    @RequiresPermissions("module:subscriber:edit")
    @GetMapping("/authSubMeter/{subscriberId}")
    public String authSubMeter(@PathVariable("subscriberId") String subscriberId, ModelMap mmap)
    {
        mmap.put("subscriberId", subscriberId);
        return prefix + "/authSubMeter";
    }

    /**
     * 查询已分配热表角色列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/authSubMeter/allocatedSubList")
    @ResponseBody
    public TableDataInfo allocatedSubList(BaseCalorimeterData calorimeter)
    {
        startPage();
        List<BaseCalorimeterData> list = calorimeterDataService.selectAllocatedSubList(calorimeter);
        return getDataTable(list);
    }

    /**
     * 取消授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubMeter/cancel")
    @ResponseBody
    public AjaxResult cancelAuthSubMeter(LinkSubMeter subMeter)
    {
        return toAjax(baseSubscriberService.deleteAuthSubMeter(subMeter));
    }

    /**
     * 批量取消授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubMeter/cancelAll")
    @ResponseBody
    public AjaxResult cancelAuthSubMeterAll(String subscriberId, String calorimeterIds)
    {
        return toAjax(baseSubscriberService.deleteAuthSubMeters(subscriberId, calorimeterIds));
    }

    /**
     * 选择热表
     */
    @GetMapping("/authSubMeter/selectSubMeter/{subscriberId}")
    public String selectSubMeter(@PathVariable("subscriberId") String subscriberId, ModelMap mmap)
    {
        mmap.put("subscriberId", subscriberId);
        return prefix + "/selectSubMeter";
    }

    /**
     * 查询未分配热表列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/authSubMeter/unallocatedSubList")
    @ResponseBody
    public TableDataInfo unallocatedSubList(BaseCalorimeterData baseCalorimeterData)
    {
        startPage();
        List<BaseCalorimeterData> list = calorimeterDataService.selectUnallocatedSubList(baseCalorimeterData);
        return getDataTable(list);
    }

    /**
     * 批量选择热表授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubMeter/selectAll")
    @ResponseBody
    public AjaxResult selectAuthSubMeterAll(String subscriberId, String calorimeterIds)
    {
        return toAjax(baseSubscriberService.insertAuthSubMeters(subscriberId, calorimeterIds));
    }
    /**
     * 分配阀门
     */
    @RequiresPermissions("module:subscriber:edit")
    @GetMapping("/authSubValve/{subscriberId}")
    public String authSubValve(@PathVariable("subscriberId") String subscriberId, ModelMap mmap)
    {
        mmap.put("subscriberId", subscriberId);
        return prefix + "/authSubValve";
    }

    /**
     * 查询已分配阀门角色列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/authSubValve/allocatedSubList")
    @ResponseBody
    public TableDataInfo allocatedSubList(BaseValveData valve)
    {
        startPage();
        List<BaseValveData> list = valveDataService.selectAllocatedSubList(valve);
        return getDataTable(list);
    }

    /**
     * 取消授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubValve/cancel")
    @ResponseBody
    public AjaxResult cancelAuthSubValve(LinkSubValve subValve)
    {
        return toAjax(baseSubscriberService.deleteAuthSubValve(subValve));
    }

    /**
     * 批量取消授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubValve/cancelAll")
    @ResponseBody
    public AjaxResult cancelAuthSubValveAll(String subscriberId, String valveIds)
    {
        return toAjax(baseSubscriberService.deleteAuthSubValves(subscriberId, valveIds));
    }

    /**
     * 选择阀门
     */
    @GetMapping("/authSubValve/selectSubValve/{subscriberId}")
    public String selectSubValve(@PathVariable("subscriberId") String subscriberId, ModelMap mmap)
    {
        mmap.put("subscriberId", subscriberId);
        return prefix + "/selectSubValve";
    }

    /**
     * 查询未分配阀门列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/authSubValve/unallocatedSubList")
    @ResponseBody
    public TableDataInfo unallocatedSubList(BaseValveData baseValveData)
    {
        startPage();
        List<BaseValveData> list = valveDataService.selectUnallocatedSubList(baseValveData);
        return getDataTable(list);
    }

    /**
     * 批量选择阀门授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubValve/selectAll")
    @ResponseBody
    public AjaxResult selectAuthSubValveAll(String subscriberId, String valveIds)
    {
        return toAjax(baseSubscriberService.insertAuthSubValves(subscriberId, valveIds));
    }
    /**
     * 分配室温采集器
     */
    @RequiresPermissions("module:subscriber:edit")
    @GetMapping("/authSubTemp/{subscriberId}")
    public String authSubTemp(@PathVariable("subscriberId") String subscriberId, ModelMap mmap)
    {
        mmap.put("subscriberId", subscriberId);
        return prefix + "/authSubTemp";
    }

    /**
     * 查询已分配室温采集器角色列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/authSubTemp/allocatedSubList")
    @ResponseBody
    public TableDataInfo allocatedSubList(BaseRoomTemp temp)
    {
        startPage();
        List<BaseRoomTemp> list = roomTempService.selectAllocatedSubList(temp);
        return getDataTable(list);
    }

    /**
     * 取消授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubTemp/cancel")
    @ResponseBody
    public AjaxResult cancelAuthSubTemp(LinkSubTemp subTemp)
    {
        return toAjax(baseSubscriberService.deleteAuthSubTemp(subTemp));
    }

    /**
     * 批量取消授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubTemp/cancelAll")
    @ResponseBody
    public AjaxResult cancelAuthSubTempAll(String subscriberId, String brtIds)
    {
        return toAjax(baseSubscriberService.deleteAuthSubTemps(subscriberId, brtIds));
    }

    /**
     * 选择室温采集器
     */
    @GetMapping("/authSubTemp/selectSubTemp/{subscriberId}")
    public String selectSubTemp(@PathVariable("subscriberId") String subscriberId, ModelMap mmap)
    {
        mmap.put("subscriberId", subscriberId);
        return prefix + "/selectSubTemp";
    }

    /**
     * 查询未分配室温采集器列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/authSubTemp/unallocatedSubList")
    @ResponseBody
    public TableDataInfo unallocatedSubList(BaseRoomTemp baseRoomTemp)
    {
        startPage();
        List<BaseRoomTemp> list = roomTempService.selectUnallocatedSubList(baseRoomTemp);
        return getDataTable(list);
    }

    /**
     * 批量选择室温采集器授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubTemp/selectAll")
    @ResponseBody
    public AjaxResult selectAuthSubTempAll(String subscriberId, String brtIds)
    {
        return toAjax(baseSubscriberService.insertAuthSubTemps(subscriberId, brtIds));
    }
    /**
     * 分配集中器
     */
    @RequiresPermissions("module:subscriber:edit")
    @GetMapping("/authSubConcentrator/{subscriberId}")
    public String authSubConcentrator(@PathVariable("subscriberId") String subscriberId, ModelMap mmap)
    {
        mmap.put("subscriberId", subscriberId);
        return prefix + "/authSubConcentrator";
    }

    /**
     * 查询已分配集中器角色列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/authSubConcentrator/allocatedSubList")
    @ResponseBody
    public TableDataInfo allocatedSubList(BaseConcentratorData concentrator)
    {
        startPage();
        List<BaseConcentratorData> list = concentratorDataService.selectAllocatedSubList(concentrator);
        return getDataTable(list);
    }

    /**
     * 取消授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubConcentrator/cancel")
    @ResponseBody
    public AjaxResult cancelAuthSubConcentrator(LinkSubConcentrator subConcentrator)
    {
        return toAjax(baseSubscriberService.deleteAuthSubConcentrator(subConcentrator));
    }

    /**
     * 批量取消授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubConcentrator/cancelAll")
    @ResponseBody
    public AjaxResult cancelAuthSubConcentratorAll(String subscriberId, String concentratorIds)
    {
        return toAjax(baseSubscriberService.deleteAuthSubConcentrators(subscriberId, concentratorIds));
    }

    /**
     * 选择集中器
     */
    @GetMapping("/authSubConcentrator/selectSubConcentrator/{subscriberId}")
    public String selectSubConcentrator(@PathVariable("subscriberId") String subscriberId, ModelMap mmap)
    {
        mmap.put("subscriberId", subscriberId);
        return prefix + "/selectSubConcentrator";
    }

    /**
     * 查询未分配集中器列表
     */
    @RequiresPermissions("module:subscriber:list")
    @PostMapping("/authSubConcentrator/unallocatedSubList")
    @ResponseBody
    public TableDataInfo unallocatedSubList(BaseConcentratorData baseConcentratorData)
    {
        startPage();
        List<BaseConcentratorData> list = concentratorDataService.selectUnallocatedSubList(baseConcentratorData);
        return getDataTable(list);
    }

    /**
     * 批量选择集中器授权
     */
    @Log(title = "地址管理", businessType = BusinessType.GRANT)
    @PostMapping("/authSubConcentrator/selectAll")
    @ResponseBody
    public AjaxResult selectAuthSubConcentratorAll(String subscriberId, String concentratorIds)
    {
        return toAjax(baseSubscriberService.insertAuthSubConcentrators(subscriberId, concentratorIds));
    }

}
